/**
  ******************************************************************************
  * @file    SW_Execute.c
  * @author  exatron
  * @version V1.0.0
  * @date    segunda, 22 de dezembro de 2014
  * @brief   Executa um comando e devolve a saída.
  ******************************************************************************
  * @attention
  *
  *
  ******************************************************************************
  */

/** @addtogroup SW
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include <common.h>

/* External functions --------------------------------------------------------*/

/** @defgroup SW_Functions
  * @{
  */

/**
  * @brief  Executa um comando de linux.
  * @param  char *: Comando a executar
  * @retval Devolve pela serial a saída
  */
void SW_Execute(char *cmd)
{
	char temp_buf[255], line[1024], file[255];
	FILE *fp;

	bzero(temp_buf, sizeof(temp_buf));
	bzero(line, sizeof(line));
	bzero(file, sizeof(file));

	/* Caminho do arquivo de saida */
	sprintf(file, "%s%s", PATH_TEMP, "execute");

	/* Comando */
	sprintf(temp_buf, "%s 2> %s > %s", cmd, file,file);

	/* Executa */
	system(temp_buf);

	/* Abre o arquivo */
	fp = fopen(file, "r");

	/* Devolve pela serial a saida */
	while(!feof(fp))
	{
		bzero(line, sizeof(line));
		fgets(line,sizeof(line),fp);
		aux_USART0_puts("%s\r", line);
	}

	fclose(fp);

	aux_SetBusy(FALSE);
}

/**
  * @}
  */

/**
  * @}
  */

